Component({
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },
    properties: {
        multi: {
            type: Boolean,
            value: true,
            observer: '_multiChange'
        },
        extClass: {
            type: String,
            value: ''
        },
        prop: {
            type: String,
            value: ''
        }
    },
    data: {
        targetList: [],
        parentCell: null
    },
    relations: {
        '../checkbox/checkbox': {
            type: 'descendant',
            linked: function (target) {
                this.data.targetList.push(target);
                target.setMulti(this.data.multi);
                if (!this.data.firstItem) {
                    this.data.firstItem = target;
                }
                if (target !== this.data.firstItem) {
                    target.setOuterClass('weui-cell_wxss');
                }
            },
            unlinked: function (target) {
                var index = -1;
                this.data.targetList.forEach(function (item, idx) {
                    if (item === target) {
                        index = idx;
                    }
                });
                this.data.targetList.splice(index, 1);
                if (!this.data.targetList) {
                    this.data.firstItem = null;
                }
            }
        },
        '../form/form': {
            type: 'ancestor'
        },
        '../cells/cells': {
            type: 'ancestor',
            linked: function (target) {
                if (!this.data.parentCell) {
                    this.data.parentCell = target;
                }
                this.setParentCellsClass();
            },
            unlinked: function () {
                this.data.parentCell = null; // 方便内存回收
            }
        }
    },
    methods: {
        checkedChange: function (checked, target) {
            if (this.data.multi) {
                var vals_1 = [];
                this.data.targetList.forEach(function (item) {
                    if (item.data.checked) {
                        vals_1.push(item.data.value);
                    }
                });
                this.triggerEvent('change', { value: vals_1 });
            }
            else {
                var val_1 = '';
                this.data.targetList.forEach(function (item) {
                    if (item === target) {
                        val_1 = item.data.value;
                    }
                    else {
                        item.setData({
                            checked: false
                        });
                    }
                });
                this.triggerEvent('change', { value: val_1 }, {});
            }
        },
        setParentCellsClass: function () {
            var className = this.data.multi ? 'weui-cells_checkbox' : '';
            if (this.data.parentCell) {
                this.data.parentCell.setCellsClass(className);
            }
        },
        _multiChange: function (multi) {
            this.data.targetList.forEach(function (target) {
                target.setMulti(multi);
            });
            if (this.data.parentCell) {
                this.data.parentCell.setCellMulti(multi);
            }
            return multi;
        }
    }
});
